# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]/\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ 1 ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls -t --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alFr'
alias la='ls -A'
alias l='ls -CF'

alias vi=vim

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

set -o vi
bind 'set completion-ignore-case on'
bind 'set show-all-if-ambiguous on'
export EDITOR=vim

# http://www.geocities.com/h2428/petar/bash_acd.htm
#
# do ". acd_func.sh"
# acd_func 1.0.5, 10-nov-2004
# petar marinov, http:/geocities.com/h2428, this is public domain

cd_func ()
{
  local x2 the_new_dir adir index
  local -i cnt

  if [[ $1 ==  "--" ]]; then
    dirs -v
    return 0
  fi

  the_new_dir=$1
  [[ -z $1 ]] && the_new_dir=$HOME

  if [[ ${the_new_dir:0:1} == '-' ]]; then
    #
    # Extract dir N from dirs
    index=${the_new_dir:1}
    [[ -z $index ]] && index=1
    adir=$(dirs +$index)
    [[ -z $adir ]] && return 1
    the_new_dir=$adir
  fi

  #
  # '~' has to be substituted by ${HOME}
  [[ ${the_new_dir:0:1} == '~' ]] && the_new_dir="${HOME}${the_new_dir:1}"

  #
  # Now change to the new dir and add to the top of the stack
  pushd "${the_new_dir}" > /dev/null
  [[ $? -ne 0 ]] && return 1
  the_new_dir=$(pwd)

  #
  # Trim down everything beyond 11th entry
  popd -n +11 2>/dev/null 1>/dev/null

  #
  # Remove any other occurence of this dir, skipping the top of the stack
  for ((cnt=1; cnt <= 10; cnt++)); do
    x2=$(dirs +${cnt} 2>/dev/null)
    [[ $? -ne 0 ]] && return 0
    [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
    if [[ "${x2}" == "${the_new_dir}" ]]; then
      popd -n +$cnt 2>/dev/null 1>/dev/null
      cnt=cnt-1
    fi
  done

  return 0
}

alias cd=cd_func
alias d='cd --'
alias 1='cd -'
alias 2='cd -2'
alias 3='cd -3'
alias 4='cd -4'
alias 5='cd -5'
alias 6='cd -6'
alias 7='cd -7'
alias 8='cd -8'
alias 9='cd -9'

alias cdh7='cd device/softwinner/astar-h7'
alias cdsys='cd ../lichee/tools/pack/chips/sun8iw5p1/configs/h7'
alias cd2='cd ../../'
alias cd3='cd ../../..'
alias cd4='cd ../../../..'
alias cd5='cd ../../../../..'
alias cd6='cd ../../../../../..'
alias cd7='cd ../../../../../../..'
alias cd8='cd ../../../../../../../..'


if [[ $BASH_VERSION > "2.05a" ]]; then
  # ctrl+w shows the menu
  bind -x "\"\C-a\":cd_func -- ;"
fi

#forced to use 256 color
alias tmux="tmux -2"

alias ag='ag --path-to-ignore ~/.agignore'

dirdiff() {
    #do things with parameters like $1 such as
    vim -c ':colorscheme railscasts' -c ":DirDiff "$1" "$2""
}


fd(){
    find . -name ".repo" -prune -o -name ".git" -prune -o -name "out" -prune -o -iname "$1" -print
}


jf(){
    if [ ! -z "$1" ]; then
        fg "$1"
    else
        fg
    fi
}

tgz(){
    src="$1"

    if [ -d $src ];  
    then  
        #$src is a direstory.
        dst=${src%/*}
    else  
        #"$src is a file.";  
        dst=${src%.*}
    fi
    
    tmp=$dst".tgz"
    if [ -e $tmp ] ;  
    then
        echo "${dst}.tgz is alrealy exist!!!"
    else
        tar -zcf "$dst".tgz "$1"
    fi
}


txz(){
    tar -zxf "$1"
}



sdiff() {
    svn diff -r "$1":"$2" "$3"
}


sdirdiff() {
    svn diff -r "$1":"$2" --summarize
}


csc() {
    if [ ! -z "$8" ]; then
        find "$8" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$7" ]; then
        find "$7" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$6" ]; then
        find "$6" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$5" ]; then
        find "$5" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$4" ]; then
        find "$4" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$3" ]; then
        find "$3" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$2" ]; then
        find "$2" -name "*.h" -o -name "*.c" >> cscope.files
    fi
    if [ ! -z "$1" ]; then
        find "$1" -name "*.h" -o -name "*.c" >> cscope.files
        sed -i 's/^\.\///' cscope.files
        cscope -bq ; ctags --sort=foldcase -L cscope.files
    else
        echo ""
        echo "Please enter the path !!!!"
        echo ""
    fi
}

cscpp() {
    if [ ! -z "$8" ]; then
        find "$8" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$7" ]; then
        find "$7" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$6" ]; then
        find "$6" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$5" ]; then
        find "$5" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$4" ]; then
        find "$4" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$3" ]; then
        find "$3" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$2" ]; then
        find "$2" -name "*.h" -o -name "*.cpp" >> cscope.files
    fi
    if [ ! -z "$1" ]; then
        find "$1" -name "*.h" -o -name "*.cpp" >> cscope.files
        sed -i 's/^\.\///' cscope.files
        cscope -bq ; ctags --sort=foldcase -L cscope.files
    else
        echo ""
        echo "Please enter the path !!!!"
        echo ""
    fi
}

csjava() {
    if [ ! -z "$8" ]; then
        find "$8" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$7" ]; then
        find "$7" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$6" ]; then
        find "$6" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$5" ]; then
        find "$5" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$4" ]; then
        find "$4" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$3" ]; then
        find "$3" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$2" ]; then
        find "$2" -name "*.java" >> cscope.files
    fi
    if [ ! -z "$1" ]; then
        find "$1" -name "*.java" >> cscope.files
        sed -i 's/^\.\///' cscope.files
        cscope -bq ; ctags --sort=foldcase -L cscope.files
    else
        echo ""
        echo "Please enter the path !!!!"
        echo ""
    fi
}

csall() {
    if [ ! -z "$8" ]; then
        find "$8" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$7" ]; then
        find "$7" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$6" ]; then
        find "$6" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$5" ]; then
        find "$5" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$4" ]; then
        find "$4" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$3" ]; then
        find "$3" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$2" ]; then
        find "$2" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    fi
    if [ ! -z "$1" ]; then
        find "$1" -name "*.h" -o -name "*.cpp" -o -name "*.c" -o -name "*.java" >> cscope.files
    #    sed -i 's/^\.\///' cscope.files
         cscope -bq ; ctags -L cscope.files
    else
        echo ""
        echo "Please enter the path !!!!"
        echo ""
    fi
}

csclear() {
    rm cscope* tags
}

lsh(){
        echo "----------------------------------------ls help------------------------------"
        echo "           ll -tr                                                    :sort by mod time, -r is reverse direction"
        echo '           ls --ignore={"*.c","*.h"}                                 :list files ignore .c and .h files '
        echo '                eg:   rm -rf `ls --ignore={"*.c","*.h"}`                        :delete the files         '
        echo '                      ls --ignore={"*.c","*.h"}|xargs -i rm -rf {}              :delete the files         '
        echo "           ls -d  */                                                 :-d is not show directory content; */ is just list directory,not file!"
        echo "           ll -d  \.*                                                :just list files started with . and * means include file and directory"
        echo "           ll -d  \.*/                                               :just list files started with . but just include directory"
        echo "-----------------------------------------------------------------------------"
}

fdh(){
        echo "----------------------------------------find help------------------------------"
        echo ""
        echo "    //ignore some files, donot forget * ,it is mandatory. note: there are two same -path !!"
        echo ""
        echo '     find . -iname "*.h" ! -path "./arch/arm/*" -o -iname "*.c" ! -path "./arch/arm/*" '
        echo ""
        echo '     find -name "BoardConfig.mk" -prune -o -type f -print     '
        echo ""
        echo ""
        echo "-----------------------------------------------------------------------------"
}

agh(){
        echo "----------------------------------------ag help------------------------------"
        echo ""
        echo '           --ignore={"*.java",   "*.html",   "*.*.html"}             use wildcard character     '
        echo ""
        echo '                      -G   ".*.java"                         use RegExp  '
        echo ""
        echo "                      -l                         only list file name    "
        echo ""
        echo "-----------------------------------------------------------------------------"
}

teeh(){
        echo "----------------------------------------tee help------------------------------"
        echo ""
        echo '                       xxxx 2>&1 | tee xxx.log '
        echo ""
        echo '                       example: mm -j4 2>&1 | tee ~/kkk.log '
        echo ""
        echo '                       2:stderr  1:stdout;    so "2>&1" will put all log to kkk.log    '
        echo ""
        echo "-----------------------------------------------------------------------------"
}
#source ~/.bashrc_marks

vimlock() {
	vim -c 'cmap wq <NOP>' -c 'cmap q <NOP>' -c 'cmap qa! <NOP>'
}

#unalias rm 2>/dev/null
#rm_opts=""
#
#if [ -x /usr/local/bin/safe-rm ]; then
#    real_rm=/usr/local/bin/safe-rm
#elif [ -x ~/local/bin/safe-rm ]; then
#    real_rm=~/local/bin/safe-rm
#else
#    real_rm=/bin/rm
#fi
#
#function confirm {
#  echo -n "Do you want to continue (Y/N)? "
#  read v
#  v=$(echo $v|tr '[a-z]' '[A-Z]')
#  if [[ "$v" == "Y" ]]; then
#    return 0
#  elif [[ "$v" == "N" ]]; then
#    return 1
#  else
#    confirm
#  fi
#}
#
#function dm {
#
#  if [ $# -lt 1 ]; then
#      echo "please input path!!"
#      return
#  fi
#
#  if [ $# -lt 2 ]; then
#      echo "please input the files you want to delete!!"
#      return
#  fi
#
#  if [[ $1 == '.' ]]; then
#      echo "You are in: $(pwd)"
#      confirm
#    if [ $? -eq 1 ]; then
#        return
#    fi
#  fi
#
#  #${@:2} all parameters after the 1st parameter
#  cd $1 && $real_rm -rf ${@:2}
#
#}


#function rm {
#  echo ""
#  echo "Don't use rm, please use dm instead!!"
#  echo "Or add the prefix to use it: /bin/rm "
#  echo ""
#  #if [ $# -gt 2 ]; then
#  #  echo "WARNING: You have passed a list of files and directories that is $# entries long!  Is this what you intended?"
#  #  echo "Here is the list of files:"
#  #  echo "$@"
#  #  confirm
#  #  if [ $? -eq 0 ]; then
#  #    $real_rm $rm_opts $@
#  #  fi
#  #else
#  #  $real_rm $rm_opts $@
#  #fi
#}


#export ALL_PROXY=socks5://127.0.0.1:1080
#export ALL_PROXY=socks5://192.168.15.85:1080
#export http_proxy=http://127.0.0.1:8118
#export https_proxy=http://127.0.0.1:8118
